package SEApp.components {
	import SEApp.se_app;
	import SEApp.se_util;
	
	import UIFree.controls.base.uif_source;
	import UIFree.controls.uif_label;
	import UIFree.controls.uif_leavesBlock;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import chBox.chb_button;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class se_userView extends uif_leavesBlock {
		private var _avatar				: uif_source;
		private var _title				: uif_label;
		private var _myProfileButton	: chb_button;
		public function se_userView(parent:uif_component, ax:int=0, ay:int=0, 
									awidth:int=0, aheight:int=0, styleClass:String='',
									myProfileClickEvent: String='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			createComponents(myProfileClickEvent);
		}
		
		protected function createComponents(myProfileClickEvent: String): void {
			var inner: Rectangle = block.getInner();
			_avatar = new uif_source(block, inner.x, inner.y, inner.width, inner.width, block.style['avatar']);
			_myProfileButton = new chb_button(block, 'мой профиль', inner.x, inner.y + inner.height + 10, inner.width, function(e: Event): void {
				dispatchEvent(new Event(myProfileClickEvent));
			});
			
			inner.top += inner.width;
			inner.bottom -= _myProfileButton.height;
			_title = new uif_label(block, inner.x, inner.y, inner.width, inner.height, block.style['title']);
		}
		
		public function get avatar(): uif_source {
			return _avatar;
		}
		
		public function get data(): Object {
			return _data;			
		}
		
		public function set data(a_data: Object): void {
			if (_data != a_data) {
				_data = a_data;
				update();
				show();
			}
		}
		
		protected function update(): void {
			_avatar.loadSource(data.pic);
			_title.text = se_util.userName(data);
			
			var inner: Rectangle = block.getInner();
			var sto: Number = inner.y + inner.height - _myProfileButton.height;
			if (data.uid == (app as se_app).externalServer.viewer_id)
				sto = inner.y + inner.height + 10;
			_myProfileButton.fx('y', _myProfileButton.y, sto).start();
		}
	}
}